﻿using System;
using System.IO;
using System.ServiceProcess;
using Netcare.Core;
using Netcare.Web.Host;
using Netcare.Web.Host.Linux;
using Netcare.Web.Host.NT;

namespace Netcare.Agent
{
    public partial class TService : ServiceBase
    {
        private IWebHost host;

        public TService()
        {
            InitializeComponent();   
        }


        protected override void OnStart(string[] args)
        {
            Log.WriteInfo("Try to start Netcare.Agent.Service");

            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                host = AspHost.Create(55501, "/", Directory.GetCurrentDirectory());
            }
            else if (Environment.OSVersion.Platform == PlatformID.Unix)
            {
                host = XspHost.Create(55501, "/", Directory.GetCurrentDirectory());
            }
            else
                throw new PlatformNotSupportedException(); 
            
            host.Start();
            Log.WriteInfo("Netcare.Agent.Service started successfully");
        }

        protected override void OnStop()
        {
            Log.WriteInfo("Try to stop Netcare.Agent.Service");
            host.Stop();
            Log.WriteInfo("Netcare.Agent.Service stopped successfully");
        }
    }
}
